package  persentation.presentDepartmentDean;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JButton;

import java.awt.Font;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JLabel;

import vo.frameworkVO.FrameworkVO;
import vo.teachPlanVO.TeachPlanVO;
import businesslogic.frameworkBl.FrameworkController;
import businesslogic.teachPlanBl.TeachPlanController;


public class InputTeachPlanPanel extends JPanel implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTable table;
	private JTable table_1;
	private JComboBox comboBox;
	private JButton btnNewButton,btnNewButton_1;
	private JScrollPane jScrollPane,jScrollPane2;
	private Vector data,data2,columnName;
	private MyModel model,model_1;
	private TeachPlanController teachControl;
	private String department;
	private int tongshi,kaifang,zhuanye;
	private JLabel label_3;
	private JLabel label_4;
	private JLabel label_5;
	
	public InputTeachPlanPanel(TeachPlanController tc,String depart){
		teachControl=tc;
		department=depart;
		setSize(900, 690);
		setLayout(null);
		comboBox = new JComboBox();
		comboBox.setBounds(43, 12, 113, 46);
		comboBox.setModel(new DefaultComboBoxModel(new String[] { "", "第一学期",
				"第二学期", "第三学期", "第四学期", "第五学期", "第六学期", "第七学期", "第八学期" }));
		add(comboBox);
		comboBox.addActionListener(this);
		comboBox.setBorder(BorderFactory.createTitledBorder("学期教学计划"));

		columnName=new Vector();
		columnName.add("课程编号");
		columnName.add("课程名称");
		columnName.add("课程性质");
		columnName.add("学分");
		columnName.add("院系");
		
		data=new Vector();
		model = new MyModel(data, columnName);
		table = new JTable(model);
		table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		table.setFillsViewportHeight(true);
		table.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if ((table.getSelectedRow()!=-1) && (comboBox.getSelectedIndex()!=0)){
					btnNewButton_1.setEnabled(true);
				}
			}
		});
		jScrollPane = new JScrollPane(table);
		jScrollPane.setBounds(0, 70, 385, 307);
		add(jScrollPane);

		btnNewButton = new JButton("<-添加");
		btnNewButton.setBounds(386, 181, 71, 28);
		add(btnNewButton);
		btnNewButton.setEnabled(false);
		btnNewButton.addActionListener(this);

		btnNewButton_1 = new JButton("删除->");
		btnNewButton_1.setBounds(386, 240, 71, 28);
		add(btnNewButton_1);
		btnNewButton_1.setEnabled(false);
		btnNewButton_1.addActionListener(this);
		
		model_1 = new MyModel();
		table_1 = new JTable(model_1);
		table_1.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		table_1.setFillsViewportHeight(true);
		jScrollPane2 = new JScrollPane(table_1);
		jScrollPane2.setBounds(459, 70, 391, 307);
		add(jScrollPane2);
		table_1.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				if ((table_1.getSelectedRow()!=-1) && (comboBox.getSelectedIndex()!=0)){
					btnNewButton.setEnabled(true);
				}
			}
		});
		updateRightTable();
		
		JLabel lblNewLabel = new JLabel("现有通识通修学分：");
		lblNewLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
		lblNewLabel.setBounds(69, 469, 149, 18);
		add(lblNewLabel);
		
		JLabel label = new JLabel("现有学科专业学分：");
		label.setFont(new Font("微软雅黑", Font.BOLD, 16));
		label.setBounds(326, 469, 149, 18);
		add(label);
		
		JLabel label_1 = new JLabel("现有开放选修学分：");
		label_1.setFont(new Font("微软雅黑", Font.BOLD, 16));
		label_1.setBounds(593, 469, 161, 18);
		add(label_1);
		
		JLabel label_2 = new JLabel("未添加课程");
		label_2.setForeground(new Color(153, 204, 255));
		label_2.setFont(new Font("微软雅黑", Font.BOLD, 16));
		label_2.setBounds(459, 40, 131, 18);
		add(label_2);
		
		label_3 = new JLabel("0");
		label_3.setForeground(Color.RED);
		label_3.setFont(new Font("微软雅黑", Font.BOLD, 16));
		label_3.setBounds(228, 469, 47, 18);
		add(label_3);
		
		label_4 = new JLabel("0");
		label_4.setForeground(Color.RED);
		label_4.setFont(new Font("微软雅黑", Font.BOLD, 16));
		label_4.setBounds(485, 469, 42, 18);
		add(label_4);
		
		label_5 = new JLabel("0");
		label_5.setForeground(Color.RED);
		label_5.setFont(new Font("微软雅黑", Font.BOLD, 16));
		label_5.setBounds(749, 469, 42, 18);
		add(label_5);
		
		JLabel label_6 = new JLabel("所需通识通修学分：");
		label_6.setFont(new Font("微软雅黑", Font.BOLD, 16));
		label_6.setBounds(69, 423, 144, 18);
		add(label_6);
		
		JLabel label_7 = new JLabel("所需学科专业学分：");
		label_7.setFont(new Font("微软雅黑", Font.BOLD, 16));
		label_7.setBounds(326, 423, 149, 18);
		add(label_7);
		
		JLabel label_8 = new JLabel("0");
		label_8.setForeground(Color.RED);
		label_8.setFont(new Font("微软雅黑", Font.BOLD, 16));
		label_8.setBounds(228, 423, 84, 18);
		add(label_8);
		
		JLabel label_9 = new JLabel("0");
		label_9.setForeground(Color.RED);
		label_9.setFont(new Font("微软雅黑", Font.BOLD, 16));
		label_9.setBounds(485, 423, 82, 18);
		add(label_9);
		
		JLabel label_10 = new JLabel("所需开放选修学分：");
		label_10.setFont(new Font("微软雅黑", Font.BOLD, 16));
		label_10.setBounds(593, 423, 161, 18);
		add(label_10);
		
		JLabel label_11 = new JLabel("0");
		label_11.setForeground(Color.RED);
		label_11.setFont(new Font("微软雅黑", Font.BOLD, 16));
		label_11.setBounds(749, 423, 86, 18);
		add(label_11);

		calCredit();
		
		FrameworkController frameworkController=new FrameworkController();
		try {
			FrameworkVO v=frameworkController.find();
			label_8.setText(Integer.toString(v.getCompulsoryLow())+"--"+Integer.toString(v.getCompulsoryHigh()));
			label_9.setText(Integer.toString(v.getCoreLow())+"--"+Integer.toString(v.getCoreHigh()));
			label_11.setText(Integer.toString(v.getChooseLow())+"--"+Integer.toString(v.getChooseHigh()));
			
		} catch (Exception e) {
			DepartmentdeanExceptionHandler.networkException();
		}
		
	}

	@Override
	public void actionPerformed(ActionEvent e){
		try {
			if (e.getSource()==comboBox){
				updateLeftTable();
				if (comboBox.getSelectedIndex()==0){
					btnNewButton.setEnabled(false);
					btnNewButton_1.setEnabled(false);
				} else {
					if (table_1.getSelectedRow()!=-1){
						btnNewButton.setEnabled(true);
					}
				}
				if (table.getSelectedRow()!=-1){
					btnNewButton_1.setEnabled(true);
				} else {
					btnNewButton_1.setEnabled(false);
				}
			} else if (e.getSource()==btnNewButton){
				String couID=(String)model_1.getValueAt(table_1.getSelectedRow(), 0);
				String semester= (String)comboBox.getSelectedItem();
				TeachPlanVO teachPlanVO=new TeachPlanVO(couID, null, 0, department, null, semester);
				teachControl.addTeachPlan(teachPlanVO);
				updateLeftTable();
				updateRightTable();
				
			} else if (e.getSource()==btnNewButton_1){
				String couID=(String)model.getValueAt(table.getSelectedRow(), 0);
				String semester= (String)comboBox.getSelectedItem();
				TeachPlanVO teachPlanVO=new TeachPlanVO(couID, null, 0, department, null, semester);
				teachControl.removeTeachPlan(teachPlanVO);
				updateLeftTable();
				updateRightTable();
				if (table.getSelectedRow()!=-1){
					btnNewButton_1.setEnabled(true);
				} else {
					btnNewButton_1.setEnabled(false);
				}
				if (table_1.getSelectedRow()==-1){
					btnNewButton.setEnabled(false);
				}
			}
		} catch (Exception e2) {
			e2.printStackTrace();
			DepartmentdeanExceptionHandler.networkException();
		}
		
	}

	public void updateRightTable(){
		try {
			data2=new Vector();
			ArrayList<TeachPlanVO> teachPlanVOs=new ArrayList<TeachPlanVO>();
			teachPlanVOs=teachControl.getUnselectedCourse(department);
			fillData(teachPlanVOs,data2);
			model_1=new MyModel(data2, columnName);
			table_1.setModel(model_1);
		} catch (Exception e) {
			DepartmentdeanExceptionHandler.networkException();
		}
		
	}
	
	public void updateLeftTable(){
		try {
			data=new Vector();
			ArrayList<TeachPlanVO> teachPlanVOs=null;
			switch (comboBox.getSelectedIndex()) {
			case 0:
				
				break;
			case 1:
				teachPlanVOs=teachControl.getPlanBySemester("第一学期",department);
				break;
			case 2:
				teachPlanVOs=teachControl.getPlanBySemester("第二学期",department);
				break;
			case 3:
				teachPlanVOs=teachControl.getPlanBySemester("第三学期",department);
				break;
			case 4:
				teachPlanVOs=teachControl.getPlanBySemester("第四学期",department);
				break;
			case 5:
				teachPlanVOs=teachControl.getPlanBySemester("第五学期",department);
				break;
			case 6:
				teachPlanVOs=teachControl.getPlanBySemester("第六学期",department);
				break;
			case 7:
				teachPlanVOs=teachControl.getPlanBySemester("第七学期",department);
				break;
			case 8:
				teachPlanVOs=teachControl.getPlanBySemester("第八学期",department);
				break;
			case 9:
				teachPlanVOs=teachControl.getPlanBySemester("第九学期",department);
				break;
			case 10:
				teachPlanVOs=teachControl.getPlanBySemester("第十学期",department);
				break;
			default:
				break;
			}	
			fillData(teachPlanVOs,data);
			model=new MyModel(data,columnName);
			table.setModel(model);;
			calCredit();
//			jScrollPane.setViewportView(table);
			if (table.getSelectedRow()!=-1){
				btnNewButton_1.setEnabled(true);
			} else {
				btnNewButton_1.setEnabled(false);
			}
			if (table_1.getSelectedRow()==-1){
				btnNewButton.setEnabled(false);
			}
		} catch (Exception e) {
			DepartmentdeanExceptionHandler.networkException();
		}
		
	}
	
	private void fillData(ArrayList<TeachPlanVO> teachPlanVOs,Vector myData){
		if (teachPlanVOs==null) return;
		for (TeachPlanVO t:teachPlanVOs){
			Vector row=new Vector();
			row.add(t.getCourseId());
			row.add(t.getCourseName());
			row.add(t.getAttribute());
			row.add(t.getCredit());
			row.add(t.getDepartment());
			myData.add(row);
		}
	}



	private void calCredit(){
		try {
			ArrayList<TeachPlanVO> t=teachControl.getPlanByDepartment(department);
			System.out.println(department);
			tongshi=0;
			kaifang=0;
			zhuanye=0;
			if (t==null) return;
			for (TeachPlanVO tea:t){
				if (tea.getAttribute()==null){
					continue;
				}
				if (tea.getAttribute().equals("通识通修")){
					tongshi+=tea.getCredit();
				}
				if (tea.getAttribute().equals("开放选修")){
					kaifang+=tea.getCredit();
				}
				if (tea.getAttribute().equals("学科专业")){
					zhuanye+=tea.getCredit();
				}
			}
			label_3.setText(Integer.toString(tongshi));
			label_4.setText(Integer.toString(zhuanye));
			label_5.setText(Integer.toString(kaifang));
		} catch (Exception e) {
			DepartmentdeanExceptionHandler.networkException();
		}
		
	}
}
